<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: skyblue;
        }

        span {
            /* 动画一定要加定位 */
            position: absolute;
            left: 0;
            top: 200px;
            display: block;
            width: 150px;
            height: 150px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <button>开启</button>
    <button>开启2</button>
    <div>一路向北</div>
    <span>龙卷风</span>

    <script>
        /* 
          bug：  1. 速度会叠加
                 2. 点击后会继续往前走
        */
        let btn = document.querySelectorAll('button')
        let div = document.querySelector('div')
        let span = document.querySelector('span')

        btn[0].addEventListener('click', function () {
               move(div,500)
        });
        btn[1].addEventListener('click', function () {
               move(span,300)
        });

        function move(obj, target) {
            clearInterval(obj.timer);
            obj.timer = setInterval(() => {
                let left = obj.offsetLeft;
                if (left >= target) {
                    clearInterval(obj.timer)
                    return;
                }
                obj.style.left = left + 50 + 'px';
            }, 500);
        }
    </script>
</body>

</html>